<?php
/**
 * 景点
 */
namespace App\Http\Controllers\Admin\Business;

use App\Area;
use App\Http\Controllers\Admin\Business\Service\Combo;
use App\Http\Controllers\Admin\Business\Service\ComboActivities;
use App\Http\Controllers\Admin\Business\Repository\ComboActivities as ComboActivitiesRepository;
use App\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class ComboActivitiesController extends Controller
{
    private $manager;
    private $repository;
    private $request;
    private $viewPrefix = 'admin.business.comboActivities.';
    private $routePrefix = 'admin.business.comboActivities';
    public function __construct (Request $request)
    {
        $this->manager = new ComboActivities();
        $this->repository = new ComboActivitiesRepository();
        $this->request = $request;
    }

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{

            return view($this->viewPrefix.'index',[
                'status' =>    $this->manager->getStatusList(),
                'manager'   =>  $this->manager,
                'routePrefix'   =>  $this->routePrefix,
                'rate_types' =>  $this->manager->getRateTypeList(),
                'types' =>  $this->manager->getTypeList(),
            ]);
        }

    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){
        return $this->page_json_render($this->manager->getPageList($this->request));
    }
    public function read($id){
        $model  = $this->manager->getComboActivities($id);
        $sysRate = [
            'rate'  =>  Setting::getConfig('rate','分销设置'),
            'top_rate'  =>  Setting::getConfig('topRate','分销设置')
        ];
        $reduce_type = ComboActivities::REDUCE;
        return view($this->viewPrefix.'read',[
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix,
            'model' =>  $model,
            'rate_type_user' =>  Combo::RATE_TYPE_USER,
            'sysRate'   =>  $sysRate,
            'reduce_type'   =>  $reduce_type
        ]);
    }

    /**
     * 编辑页面
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        $model = $this->manager->getComboActivities($id);
        if($model->combo) {
            $model->combo_id = $model->combo->pluck('id')->toArray();
            $model->combo_id= join(',',$model->combo_id);
        }
        return view($this->viewPrefix.'add-edit',[
            'status' =>    $this->manager->getStatusList(),
            'rate_type' =>  $this->manager->getRateTypeList(),
            'types' =>  $this->manager->getTypeList(),
            'manager'   =>  $this->manager,
            'id'    =>  $id,
            'model' =>  $model,
            'routePrefix'   =>  $this->routePrefix

        ]);
    }


    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        return view($this->viewPrefix.'add-edit',[
            'status' =>    $this->manager->getStatusList(),
            'rate_type' =>  $this->manager->getRateTypeList(),
            'types' =>  $this->manager->getTypeList(),
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix
        ]);
    }
    public function comboList(){
        $comboManager = new Combo();
        return view($this->routePrefix.'.comboList',[
            'status' =>    $comboManager->getStatusList(),
            'manager'   =>  $comboManager,
            'rate_types' =>  $comboManager->getRateTypeList(),
            'routePrefix'   =>  'admin.business.combo',
        ]);
    }
    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(){
        $this->validate($this->request,[
            'title'    =>  'required|max:50',
            'combo_id'    =>  'required',
            'activetime' =>  'required',
            'desc'  =>  'max:200'
        ]);
        $this->manager->insertOrUpdate($this->request->post());
        return redirect()->route($this->routePrefix)->with(
            $this->success()
        );
    }

    /**
     * @return array
     */
    private function draft(){
        $this->manager->draft($this->request->id);
        return  ['info'  =>  'success','msg'    =>  $this->request->id];
    }

    /**
     * @return array
     */
    private function published(){
        $this->manager->published($this->request->id);
        return  ['info'  =>  'success'];
    }

    /**
     * @return array
     */
    private function delete(){
        $this->manager->del($this->request->id);
        return  ['info'  =>  'success'];
    }

    private function sortCol(){
        $this->manager->sortCol($this->request->id,$this->request->sort);
        return  ['info'  =>  'success'];
    }
}
